<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { searchData } from '@/api/search'

export default {
  props: {
    searchData: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      error: false,
      page: 1
    }
  },

  created () {

  },

  methods: {
    async onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      try {
        const { data: res } = await searchData({
          page: this.page,
          per_page: 10,
          q: this.searchData
        })
        if (Math.random() > 0.5) {
          console.lg(1)
        }
        if (res.data.results.length === 0) {
          this.loading = false
          this.finished = true
          return
        }
        this.loading = false
        this.list.push(...res.data.results)
        this.page++
      } catch (err) {
        this.error = true
        this.loading = false
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
